; <<< Use Configuration Wizard in Context Menu >>>
;******************************************************************************
;
; startup_rvmdk.S - Startup code for Stellaris.
;
; Copyright (c) 2006-2007 Luminary Micro, Inc.  All rights reserved.
;
; Software License Agreement
;
; Luminary Micro, Inc. (LMI) is supplying this software for use solely and
; exclusively on LMI's microcontroller products.
;
; The software is owned by LMI and/or its suppliers, and is protected under
; applicable copyright laws.  All rights are reserved.  Any use in violation of
; the foregoing restrictions may subject the user to criminal sanctions under
; applicable laws, as well as to civil liability for the breach of the terms
; and conditions of this license.
;
; THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
; OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
; LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;
; This is part of revision 323 of the Stellaris boot loader.
;
;******************************************************************************

;******************************************************************************
;
; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
;
;******************************************************************************
Stack   EQU     0x00002000

;******************************************************************************
;
; Allocate space for the stack.
;
;******************************************************************************
        AREA    STACK, NOINIT, READWRITE, ALIGN=3
StackMem
        SPACE   Stack

;******************************************************************************
;
; Indicate that the code in this file preserves 8-byte alignment of the stack.
;
;******************************************************************************
        PRESERVE8

;******************************************************************************
;
; Place code into the reset code section.
;
;******************************************************************************
        AREA    RESET, CODE, READONLY
        THUMB

;******************************************************************************
;
; The vector table.
;
;******************************************************************************
Vectors
        DCD     StackMem + Stack            ; Top of Stack
        DCD     Reset_Handler - 0x20000000  ; Reset Handler
        DCD     IntDefaultHandler           ; NMI Handler
        DCD     IntDefaultHandler           ; Hard Fault Handler
        DCD     IntDefaultHandler           ; MPU Fault Handler
        DCD     IntDefaultHandler           ; Bus Fault Handler
        DCD     IntDefaultHandler           ; Usage Fault Handler
        DCD     0                           ; Reserved
        DCD     0                           ; Reserved
        DCD     0                           ; Reserved
        DCD     0                           ; Reserved
        DCD     IntDefaultHandler           ; SVCall Handler
        DCD     IntDefaultHandler           ; Debug Monitor Handler
        DCD     0                           ; Reserved
        DCD     IntDefaultHandler           ; PendSV Handler
        DCD     IntDefaultHandler           ; SysTick Handler
        DCD     IntDefaultHandler           ; GPIO Port A

;******************************************************************************
;
; This is the code that gets called when the processor first starts execution
; following a reset event.
;
;******************************************************************************
        EXPORT  Reset_Handler
Reset_Handler
    ;
    ; Call the C library enty point that handles startup.  This will copy
    ; the .data section initializers from flash to SRAM and zero fill the
    ; .bss section.  It will then call __rt_entry, which will be either the
    ; C library version or the one supplied here depending on the
    ; configured startup type.
    ;
    IMPORT  __main

    ;
    ; Copy the code from flash to RAM
    ;
    mov r0, #0
    mov r1, #0x20000000
    orr r2, r1, #0x2000
copy_loop
        ldr     r3, [r0], #4
        str     r3, [r1], #4
        cmp     r1, r2
        bne     copy_loop

    b   __main

;******************************************************************************
;
; This is the code that gets called when the processor receives an unexpected
; interrupt.  This simply enters an infinite loop, preserving the system state
; for examination by a debugger.
;
;******************************************************************************
IntDefaultHandler
    b   IntDefaultHandler

;******************************************************************************
;
; Make sure the end of this section is aligned.
;
;******************************************************************************
        ALIGN

;******************************************************************************
;
; Some code in the normal code section for initializing the heap and stack.
;
;******************************************************************************
        AREA    |.text|, CODE, READONLY

;******************************************************************************
;
; This fucntion is called by the RV-MDK C library startup code to initialize
; the C library and call the application's entry point.  For the non-C library
; version of the startup code, provide a replacement function that simply calls
; the application's entry point, avoiding the added code overhead of the C
; library initialization code.
;
;******************************************************************************
        EXPORT  __rt_entry
__rt_entry
        ;
        ; Branch to the application's entry point.
        ;
        IMPORT  main

        ldr     r0, =main
        bx      r0

;*****************************************************************************
;
; This function reads the stack pointer from the base address passed in and
; also moves the vector table to point to the vector table provided by the
; application.  Once the vector table and stack are configured this function
; reads out the start address for the application and branches to it.
;
;*****************************************************************************
        AREA    ||.text||, code, readonly, align=2
        EXPORT  CallApplication
CallApplication
        mov     r1, #0xE0000000
        orr     r1, r1, #0x0000ED00
        str     r0, [r1,#8]
        ldr     sp, [r0], #4
        ldr     r0, [r0]
        bx      r0

;******************************************************************************
;
; Make sure the end of this section is aligned.
;
;******************************************************************************
        ALIGN

;******************************************************************************
;
; Tell the assembler that we're done.
;
;******************************************************************************
        END
